package com.weitu.environmental.solidWaste.controller;


import com.alibaba.fastjson.JSON;
import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.environmental.solidWaste.entity.SolidWasteApply;
import com.weitu.environmental.solidWaste.service.SolidWasteApplyService;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.util.IDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangqingxi 2017-03-31
 */
@Controller
@RequestMapping("environmental/solidWaste/dispose")
public class SolidWasteDisposeController extends BaseController {

    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "environmental/solidWaste/dispose";

    private final SolidWasteApplyService solidWasteApplyService;

    @Autowired
    public SolidWasteDisposeController(SolidWasteApplyService solidWasteApplyService) {
        this.solidWasteApplyService = solidWasteApplyService;
    }


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView listView() {

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName(MODULE_BATH_PATH + "/list");

        return modelAndView;
    }



    @RequestMapping(value = "/dispose", method = RequestMethod.GET)
    public ModelAndView addView(String id) {

        SolidWasteApply solidWasteApply = solidWasteApplyService.findById(id);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(MODULE_BATH_PATH + "/dispose");
        modelAndView.addObject("apple", solidWasteApply);

        return modelAndView;
    }

    @RequestMapping(value = "/details", method = RequestMethod.GET)
    public ModelAndView detailsView(String id) {

        SolidWasteApply solidWasteApply = solidWasteApplyService.findById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(MODULE_BATH_PATH + "/details");
        modelAndView.addObject("apple", solidWasteApply);

        return modelAndView;
    }

    @ResponseBody
    @RequestMapping(value = "/dispose", method = RequestMethod.POST)
    public AjaxResult  dispose() {

        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数
        getEditBasicFieldMap(paramMap);

        //获得Id
        String id=paramMap.get("id").toString();

        //获得单位内部处理参数
        String unitDispose=paramMap.get("unitDispose").toString();
        String entrustDispose=paramMap.get("entrustDispose").toString();
        String exitDispose=paramMap.get("exitDispose").toString();

        List<Map> unitDisposeList =JSON.parseArray(unitDispose,Map.class);
        List<Map> entrustDisposeList =JSON.parseArray(entrustDispose,Map.class);
        List<Map> exitDisposeList =JSON.parseArray(exitDispose,Map.class);


        addParamToParamList(unitDisposeList,id);
        addParamToParamList(entrustDisposeList,id);
        addParamToParamList(exitDisposeList,id);

        Integer result=solidWasteApplyService.disposeById(unitDisposeList,entrustDisposeList,exitDisposeList,paramMap,id);

        return resultAjaxResult(result);
    }

    @ResponseBody
    @RequestMapping("/findByParam")
    public Object findByParam(HttpServletRequest request) {


        Map<String, Object> paramMap = new HashMap<String, Object>();

        //获取前台参数
        putParametersIntoMap(paramMap);
        //查询已经审批的
        paramMap.put("auditState",7);

        Integer totalNum = solidWasteApplyService.findByParamCount(paramMap);//获取总数
        DataGridPagination pagination = DataGridPagination.getInstance(request, totalNum);

        PageView pageView = pagination.getPageView();//生成pageView分页对象
        paramMap.put("pageView", pageView);

        List<SolidWasteApply> solidWasteApplyList = solidWasteApplyService.findByParam(paramMap);

        return pagination.flushDataGridData(solidWasteApplyList);
    }


    /**
     * 处理返回参数
     *
     * @param result 返回值
     * @return 返回值
     */
    private AjaxResult resultAjaxResult(Integer result) {
        AjaxResult ajaxResult = new AjaxResult();
        if (result > 0) {
            ajaxResult.setSuccess(true);
            ajaxResult.setMessage("操作成功");
        }
        return ajaxResult;
    }

    private void addParamToParamList(List<Map> paramList,String swaId){
        for(Map map: paramList){
            map.put("swaId",swaId);
            map.put("id", IDGenerator.getId());
        }
    }

}
